// pages/activity/list/activityList.js

let app = getApp();

import httpUtil, { getObjectFromStorgeAsync, KEYS} from '../../../utils/http.js'

/**
 * 需要用到async/await的页面必须加此引入
 */
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime.js'

const { $Toast } = require('../../../dist/base/index');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityList:[],
    isHasList:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getActivityList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  async getActivityList (){
    const ret = await getObjectFromStorgeAsync(KEYS.SHOP)
    const data = ret.value.data
    this.refreshNavigatior(data)
    if(!ret.state){
     wx.showToast({
       title: '请扫门店二维码',
     })
    }else{
      const { state, value } = await httpUtil.post(httpUtil.url_activityList,
       { owner: ret.value.data.tel })  
      if (state) {
        this.setData({ 
          activityList: value.data.ret, 
          isHasList: value.data.ret.length>0})
      } else {

      }
    }
  },

  /**
  * 查看活动详情
  */
  checkActivity: async function (e) {
    const ret = await getObjectFromStorgeAsync(KEYS.SHOP)
    wx.navigateTo({
      url: '../../util/webview2/webview2?id=' +
        e.currentTarget.dataset.item + '&advtip=' +
        ret.value.data.advtip + '&shopname=' + ret.value.data.shopname
    })
  },

  refreshNavigatior(shop) {
    wx.setNavigationBarTitle({
      title: shop.shopname
    });
  },


})